//
//  MineTabbar.m
//  HAHAWENWAN
//
//  Created by xiaowu on 2017/9/21.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "MineTabbar.h"

@implementation MineTabbar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.plusBtn];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 1.设置加号按钮的位置
    CGPoint temp = self.plusBtn.center;
    temp.x=self.frame.size.width/2;
    temp.y= 49 / 2;
    self.plusBtn.center=temp;
    
    // 2.设置其它UITabBarButton的位置和尺寸
    CGFloat tabbarButtonW = self.frame.size.width / 5;
    CGFloat tabbarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            // 设置宽度
            CGRect temp1=child.frame;
            temp1.size.width=tabbarButtonW;
            temp1.origin.x=tabbarButtonIndex * tabbarButtonW;
            child.frame=temp1;
            // 增加索引
            tabbarButtonIndex++;
            if (tabbarButtonIndex == 2) {
                tabbarButtonIndex++;
            }
        }
    }
}

- (void)plusClick {
    if ([self.tabBarDelegate respondsToSelector:@selector(tabBarMidButtonClick:)]) {
        [self.tabBarDelegate tabBarMidButtonClick:self];
    }
}

#pragma mark - Getters
- (UIButton *)plusBtn {
    if (!_plusBtn) {
        _plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_plusBtn setImage:[UIImage imageNamed:@"release"] forState:UIControlStateNormal];
        CGRect temp = _plusBtn.frame;
        temp.size = _plusBtn.currentImage.size;
        _plusBtn.frame=temp;
        [_plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _plusBtn;    
}

@end
